/**
 *  Copyright 2011 Miguel Ángel López Vicente 
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package cc.radio.ui;

import cc.radio.app.Capitulo;
import android.app.Activity;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class Reproductor extends Activity {

	private MediaPlayer player;
	private SeekBar barra;
	private TextView duracion;
	private Capitulo capitulo;
	

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player);
        capitulo = (Capitulo) getIntent().getSerializableExtra("capitulo");
        barra = (SeekBar) findViewById(R.id.seekbar);
        duracion = (TextView) findViewById(R.id.duracion);
           
        new Preparando().execute();
	        
	 }
	
    public void onDestroy() {
    	player.stop();
    	super.onDestroy();
    }
	
    
    
	 public void click (View v) {
		 if (player.isPlaying()) {
			 player.pause();
		 }
		 else {
			 player.start();
			 new Update().execute();
		 }
	 }
	 
	 
	 
	 private class Update extends AsyncTask<Void, Void, Void> {
		
		 public void onProgressUpdate(Void... a) {
			int d = player.getDuration()/1000;
			int p = player.getCurrentPosition()/1000;
			duracion.setText(String.format("%02d:%02d/%02d:%02d", p/60, p%60, d/60, d%60));
			barra.setProgress(player.getCurrentPosition());
		}

		protected Void doInBackground(Void... arg0) {
			while (player.isPlaying())
				try {
					Thread.sleep(1000);
					publishProgress();
				} catch (InterruptedException e) {}
			
			return null;
		}
		 
	 }

	 
	 
	 private class Seek implements OnSeekBarChangeListener {

		public void onProgressChanged(SeekBar seek, int pos, boolean user) {
			if (user)
				player.seekTo(pos);
		}

		public void onStartTrackingTouch(SeekBar arg0) {}
		public void onStopTrackingTouch(SeekBar arg0) {	}
		 
	 }
	 
	 private class Buffering implements OnBufferingUpdateListener {

		public void onBufferingUpdate(MediaPlayer player, int buffer) {
			((TextView) findViewById(R.id.buffer)).setText("Buffer: "+buffer+"%");
		}
		 
	 }	 
	 
	 private class Preparando extends AsyncTask<Void, Void, Bitmap> {

		protected Bitmap doInBackground(Void... arg0) {
	        try { 	
	        	player = new MediaPlayer();
	        	player.setDataSource(capitulo.getUrlMp3()); 	
	        	player.prepare();
	        	publishProgress();
	        	
	        	return capitulo.getBitmap();
	 
	        } catch (Exception e) {}
	        
	        return null;
		}
		
		protected void onProgressUpdate(Void... a) {
			 barra.setMax(player.getDuration());
	         barra.setOnSeekBarChangeListener(new Seek());
        	 player.setOnBufferingUpdateListener(new Buffering()); 
        	 player.start();
        	 new Update().execute();
        	 ((TextView) findViewById(R.id.titulo)).setText(capitulo.getTitulo());
		}
		 
		
		protected void onPostExecute(Bitmap a) {  	
        	if (a != null) 
        		((ImageView) findViewById(R.id.imagen)).setImageBitmap(a);
        	
		}
	 }
	 
   		
}
